package org.example.system.controller;

import org.example.system.entity.Route;
import org.example.system.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/route")
public class RouteController {
    @Autowired
    private RouteService routeService;

    @GetMapping
    public List<Route> list(@RequestParam(required = false) String name) {
        if (name != null && !name.isEmpty()) {
            return routeService.findByName(name);
        }
        return routeService.findAll();
    }

    @GetMapping("/{id}")
    public Route detail(@PathVariable Long id) {
        return routeService.findById(id);
    }

    @PostMapping
    public Map<String, Object> add(@RequestBody Route route) {
        Map<String, Object> result = new HashMap<>();
        int row = routeService.add(route);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "添加成功" : "添加失败");
        return result;
    }

    @PutMapping("/{id}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody Route route) {
        route.setId(id);
        Map<String, Object> result = new HashMap<>();
        int row = routeService.update(route);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "修改成功" : "修改失败");
        return result;
    }

    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        int row = routeService.delete(id);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "删除成功" : "删除失败");
        return result;
    }
} 